package com.alibaba.otter.node.common.autoconfigure;

import com.alibaba.otter.node.common.communication.NodeCommunicationClient;
import com.alibaba.otter.node.common.communication.NodeCommunicationEndpoint;
import com.alibaba.otter.node.common.config.ConfigClientService;
import com.alibaba.otter.shared.communication.core.CommunicationClient;
import com.alibaba.otter.shared.communication.core.impl.DefaultCommunicationClientImpl;
import com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationConnectionFactory;
import com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationEndpoint;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(NodeProperties.class)
public class NodeCommunicationAutoConfiguration {


    private final NodeProperties nodeProperties;

    public NodeCommunicationAutoConfiguration(NodeProperties nodeProperties) {
        this.nodeProperties = nodeProperties;
    }

    /**
     * node 端点服务
     */
    @Bean(initMethod = "initial",destroyMethod = "destroy")
    public DubboCommunicationEndpoint endpoint(){
        DubboCommunicationEndpoint dubboCommunicationEndpoint = new DubboCommunicationEndpoint();
        dubboCommunicationEndpoint.setPayload(nodeProperties.getCommunication().getPayload());
        return dubboCommunicationEndpoint;
    }

    /**
     * node 通讯服务
     */
    @Bean(initMethod = "initial",destroyMethod = "destroy")
    public CommunicationClient nodeProxyClient(){

        DefaultCommunicationClientImpl communicationClient = new DefaultCommunicationClientImpl();
        // 设置连接方式
        DubboCommunicationConnectionFactory dubboCommunicationConnectionFactory = new DubboCommunicationConnectionFactory();
        dubboCommunicationConnectionFactory.setPayload(nodeProperties.getCommunication().getPayload());
        // 设置dubbo通讯方式
        communicationClient.setFactory(dubboCommunicationConnectionFactory);
        communicationClient.setPoolSize(nodeProperties.getCommunication().getPoolSize());
        communicationClient.setDiscard(true);
        return communicationClient;
    }


    @Bean
    public NodeCommunicationClient nodeCommunicationClient(@Qualifier("nodeProxyClient") CommunicationClient communicationClient){

        NodeCommunicationClient nodeCommunicationClient = new NodeCommunicationClient();
        nodeCommunicationClient.setManagerAddress(nodeProperties.getManager().getAddress());
        nodeCommunicationClient.setDelegate(communicationClient);
        return nodeCommunicationClient;
    }


    @Bean
    public NodeCommunicationEndpoint nodeCommunicationEndpoint(DubboCommunicationEndpoint endpoint, ConfigClientService configClientService){

        NodeCommunicationEndpoint nodeCommunicationEndpoint = new NodeCommunicationEndpoint();
        nodeCommunicationEndpoint.setEndpoint(endpoint);
        nodeCommunicationEndpoint.setConfigClientService(configClientService);
        return nodeCommunicationEndpoint;
    }

}
